﻿<?php
session_start();

include('database.php');
include('functions.php');
include("language.php");

$_SESSION['curPage'] = 'draws.php';


if(isset($_POST['draw_id']) AND isset($_SESSION['connected']) AND $_SESSION['connected'])
{    
    if($_SESSION['drawCountDown'] > time())
    {
        $lquery = $db->prepare("SELECT * FROM draw WHERE id= ?");
        $lquery->execute(array($_POST['draw_id']));

        $data = $lquery->fetch();

        if($data['status'] == 1)
        {
            $lquery->closeCursor();
            $lquery = $db->prepare("SELECT tickets FROM user WHERE id= ?");
            $lquery->execute(array($_SESSION['id']));

            $data = $lquery->fetch();
            $lquery->closeCursor();

            if($data['tickets'] > 0)
            {
                $lquery = $db->prepare("SELECT COUNT(*) AS cnt FROM drawparticipation WHERE draw_id = ? AND user_id = ?");
                $lquery->execute(array($_POST['draw_id'], $_SESSION['id']));
                $data = $lquery->fetch();
                
                if($data['cnt'] < 10)
                {
                    $lquery = $db->prepare("INSERT INTO drawparticipation(draw_id, user_id, participationDate) VALUES (?, ?, NOW())");
                    $lquery->execute(array($_POST['draw_id'], $_SESSION['id']));
                    $lquery->closeCursor();

                    $lquery = $db->prepare("UPDATE user SET tickets = tickets - 1 WHERE id = ?");
                    $lquery->execute(array($_SESSION['id']));
                    $lquery->closeCursor();
                }
            }
        }
    }
}

function statusToString($status)
{
    include("language.php");
    switch($status)
    {
        case 0: return $draws_status_notAvailable;
        case 1: return $draws_status_drawingToCome;
    }
}

function participationStatusToString($status)
{
    include("language.php");
    switch($status)
    {
        case 0: return $draws_participationStatus_participationCancelled;
        case 1: return $draws_participationStatus_waitingForPayment;
        case 2: return $draws_participationStatus_participationValidated;
    }
}
?>

<!DOCTYPE html>
<html>
    <head>
        <title><?php echo $menuBar_drawing ?></title>
        <meta charset="utf-8" />
            <link rel="stylesheet" href="style.css" />
    </head>
    <body>
        <div class="d_body">
            <?php include("header.php"); ?>
            <?php include("menuBar.php"); ?>
            
            <?php
            
            if(isset($_SESSION['connected']) AND $_SESSION['connected'])
            {
                $lquery = $db->prepare("SELECT tickets FROM user WHERE id= ?");
                $lquery->execute(array($_SESSION['id']));

                $data = $lquery->fetch();

                $availableTickets = $data['tickets'];
                $lquery->closeCursor();

                if($availableTickets == 0)
                {
                    echo '<p>' . $draws_noTicketLeft;
                }
                else 
                {
                    echo '<p>'. $draws_youHave . $availableTickets . $draws_unusedTickets;
                }
                
                echo ' ' . $draws_buyTickets . '</p>';
                echo '<p>' . $draws_ticketsLimited . '</p>';
            }
            
            ?>
            
            <h1>
                <?php echo $draws_nextDrawing; ?>
            </h1>
                
            <?php
                
                $lquery = $db->query("SELECT * FROM draw WHERE status=1");
                
                $data = $lquery->fetch();
                
                if($lquery->rowCount() > 0)
                {       
                    $nextDrawId = $data['id'];
                    $disabled = '';

                    echo '<table class="smallDrawTable">';
                    echo    '<tr><td>';
                    echo        $draws_nextDrawingWillBe . date("d/m/Y " . $draws_at . " H:i \G\M\T+1", strtotime($data['date'])) . $draws_forAnAmountOf . parseAmount($data['amount'])  . $draws_gold . '. ';

                    if(isset($_SESSION['connected']) AND $_SESSION['connected'])
                    {
                        $lquery->closeCursor();
                        $lquery = $db->prepare("SELECT * FROM drawparticipation WHERE draw_id = ? AND user_id = ? ORDER BY ticket_id DESC");
                        $lquery->execute(array($nextDrawId, $_SESSION['id']));
                        $usedTickets = $lquery->rowCount();

                        if($lquery->rowCount() > 0) 
                        {
                            echo $draws_youAlreadyUsed . $usedTickets . $draws_ticketsForThisDrawing;
                        }
                        else 
                        {
                            echo $draws_youBoughtNoTickets;
                        }
                    }

                    echo    '</td></tr>';
                    echo    '<tr><td class="cellTextCenter">';

                    if(!isset($availableTickets) OR $availableTickets == 0 OR ($_SESSION['drawCountDown'] <= time()) OR $lquery->rowCount() >= 10)
                    {
                        $disabled = ' disabled="disabled" ';
                    }


                    echo '<form method="post" action="draws.php"><input name="draw_id" type="hidden" value="' . $nextDrawId . '" /><input type="submit" value="' . $draws_participateToDrawing . '"' . $disabled . '/></form>';

                    
                    if(isset($availableTickets))
                    {
                        if(isset($usedTickets) AND $usedTickets < 10)
                        {
                            if($availableTickets == 0)
                            {
                                echo '<tr><td class="cellTextCenter">' . $draws_needAtLeastOneTicket . '</td></tr>';
                            }
                        }
                        else echo '<tr><td class="cellTextCenter">' . $draws_maxTicketsReached . '</td></tr>';
                    }
                    else 
                    {
                        echo '<tr><td class="cellTextCenter">' . $draws_youMust . '<a href="login.php">' . $draws_log . '</a> ' . $draws_or . ' <a href="register.php">' . $draws_register . '</a> ' . $draws_toParticipate . '</td></tr>';
                    }
                    


                    echo '</td></tr></table>';
                
                }
                else 
                {
                    echo '<p>' . $draws_noNextDraw . '</p>';
                }
                
                if($lquery->rowCount() > 0 AND isset($_SESSION['connected']) AND $_SESSION['connected'])
                {
                    echo '<h3>' . $draws_unusedTicketsForNextDrawing . '</h3>';
                    
                    echo '<table class="drawTable">';
                    echo    '<tr>';
                    echo        '<th>Ticket</th>';
                    echo        '<th>' . $draws_dateOfUse . '</th>';
                    echo    '</tr>';
                    
                    while($data = $lquery->fetch())
                    {
                        echo '<tr>';
                        echo '<td class="cellTextCenter">#' . $data['ticket_id'] . '</td>';
                        echo '<td class="cellTextCenter">' . date("d/m/Y " . $draws_at . " H:i \G\M\T+1", strtotime($data['participationDate'])) . '</td>';
                        echo '</tr>';

                    }
                    echo '</table>';
                
                }

                $lquery->closeCursor();
            ?>

            <h1>
                <?php echo $draws_otherDrawings; ?>
            </h1>
            
            <?php 
                $query = $db->query("SELECT * FROM draw WHERE status = 0 ORDER BY date ASC");
                
                if($query->rowCount() > 0)
                {
            ?>
            
                    <table class="drawTable">

                        <tr>
                            <th><?php echo $draws_drawingDate; ?></th>
                            <th><?php echo $draws_drawingAmount; ?></th>
                            <th><?php echo $draws_drawingStatus; ?></th>
                        </tr>

                        <?php
                        while($data = $query->fetch())
                        {
                            echo '<tr>';
                            echo '<td class="cellTextCenter">' . date("d/m/Y " . $draws_at . " H:i \G\M\T+1", strtotime($data['date'])) . '</td>';
                            echo '<td class="cellTextCenter">' . parseAmount($data['amount']) . '</td>';
                            echo '<td class="cellTextCenter">' . statusToString($data['status']) . '</td>';
                        }

                        $query->closeCursor();
                    echo '</table>';
                }
                else 
                {
                    echo '<p>' . $draws_noOthersDraws . '</p>';
                }
                ?>

            
            
            
            <?php    
                $lquery = $db->query("SELECT battletag, date, amount, username, winner_link, d.id AS draw_id FROM draw d INNER JOIN user u ON u.id=winner_id WHERE status=2");

                if($lquery->rowCount() > 0)
                {
                    $connected = isset($_SESSION['connected']) AND $_SESSION['connected'];
            ?>
                    <h1>
                        <?php echo $draws_endedDrawings; ?>
                    </h1>
            
                    <table class="drawTable">   
                        <tr>
                            <th><?php echo $draws_drawingDate; ?></th>
                            <th><?php echo $draws_drawingAmount; ?></th>
                            <th><?php echo $draws_drawingWinner; ?></th>
                            <th><?php if($connected) { echo $draws_yourParticipation; } ?></th>
                        </tr>
                
            <?php
                    
                    
                    while($data = $lquery->fetch())
                    {
                        echo '<tr>';
                        echo '<td class="cellTextCenter">' . date("d/m/Y " . $draws_at . " H:i \G\M\T+1", strtotime($data['date'])) . '</td>';
                        echo '<td class="cellTextCenter">' . parseAmount($data['amount']) . '</td>';                   
                        echo '<td class="cellTextCenter"><a href="' . $data['winner_link'] . '">' . $data['battletag'] . '</a></td>'; 
                        if($connected) { echo '<td class="cellTextCenter"><a href="drawParticipation.php?draw_id=' . $data['draw_id'] . '">' . $draws_see . '</a></td>'; }
                    }
            ?>
                    </table>
            <?php
                }
            ?>
        </div>
        <?php include('footer.php'); ?>
    </body>
</html>